package com.team.six.bluecatshop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.six.bluecatshop.domain.*;
import com.team.six.bluecatshop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;
    @Autowired
    SkuService skuService;
    @Autowired
    ProductTypeService productTypeService;
    @Autowired
    ActivityService activityService;
    @Autowired
    MiaoShaService miaoShaService;

    /**
     * 将商品和对应的sku一次性查询出来
     * */
    @GetMapping("/list")
    public String listAll(Model model, HttpServletRequest req){
        List<Product> productList = productService.findAll();
        HttpSession session=req.getSession();


        //拿到商品的图片并将商品加载到页面------------
        for (int i=0;productList.size()>8 && i<8;i++) {//为了将sku保存在product中，遍历list集合Product p:productList
            productList.set(i,productService.setImgToProduct(productList.get(i)));

        }
        //------------------------------
        //限时购-------------------
        List<Activity> activities = activityService.findAll();
        if (activities.size()>=4){
            for(int i=0; i<4 ;i++){ //将限时购商品传到前台展示
                Product product = productService.findById(activities.get(i).getProductId());
                //System.out.println(product.getProductId());
                product=productService.setImgToProduct(product);
                activities.get(i).setProduct(product);
            }
        }else if(activities.size()>0){
            for(Activity a:activities){ //将限时购商品传到前台展示
                Product product = productService.findById(a.getProductId());
                product=productService.setImgToProduct(product);
                a.setProduct(product);
            }
        }
        //------------------------
        //秒杀------------------
        List<MiaoSha> miaoShas = miaoShaService.findAll();
        if (miaoShas.size()>=4){
            for(int i=0; i<4 ;i++){ //将限时购商品传到前台展示
                Product product = productService.findById(miaoShas.get(i).getProductId());
                //System.out.println(product.getProductId());
                product=productService.setImgToProduct(product);
                miaoShas.get(i).setProduct(product);
            }
        }else if(miaoShas.size()>0){
            for(MiaoSha a:miaoShas){ //将限时购商品传到前台展示
                Product product = productService.findById(a.getProductId());
                product=productService.setImgToProduct(product);
                a.setProduct(product);
            }
        }
        //-------------------------
        model.addAttribute("productList",productList);
        model.addAttribute("activities",activities);
        model.addAttribute("miaoShas",miaoShas);

        List<ProductType> productTypeList=productTypeService.fenlei();//加载商品分类
        session.setAttribute("productTypeList",productTypeList);

        return "index";
    }



    /**
     * 加载商品详情
     * */
    @GetMapping("/goods-details")
    public String goodsDetails(String productId,Model model){
        Product product=productService.findById(productId);//根据传来的id找到对应商品
        List<Sku> skus = skuService.findSkuByProductId(productId);//加载商品的规格
        product.setSkus(skus);//将sku放入商品中暂存
        if (activityService.findActivityByProductId(productId)!=null){
            Activity activity = activityService.findActivityByProductId(productId);
            activity.setProduct(product);
            model.addAttribute("activity",activity);
            return "rushToBuyDetails.html";
        }else if(miaoShaService.findMiaoShaByProductId(productId)!=null){

            return "redirect:/miaosha/miaosha-details?miaoshaId="+miaoShaService.findMiaoShaByProductId(productId).getMiaoshaId()+"&productId="+productId;
        }
        model.addAttribute("product",product);
        return "goods-details";
    }

    /**
     * 跳转至一级目录页面
     * */
    @GetMapping("/catalog")
    public String catalog(Model model,String ptypeId){
        List<ProductType> productTypeChildList = productTypeService.findProductTypeByParentId(ptypeId);//查二级目录
        for (ProductType productType : productTypeChildList){
            List<Product> products = productTypeService.findByProductTypeId(productType.getPtypeId());//查对应标签的商品
            productType.setProducts(products);
        }
        model.addAttribute("productTypeChildList",productTypeChildList);
        return "goods-list";
    }

    @RequestMapping("/search_product")
    public String seachProduct(String keyword, Model model){
        List<Product> productList=productService.findByKeyWord(keyword);
        //得到分页中的products条目对象
        model.addAttribute("productList",productList);
        model.addAttribute("keyword",keyword);
        return "search";
    }




    /**
     * 处理numAdd请求
     */
    @RequestMapping("/numAdd")
    @ResponseBody
    public String numAdd(String num,String productId){
        //System.out.println(num);
        //System.out.println(productId);
        Product product = productService.findById(productId);
        if(Integer.parseInt(num) >= product.getProductCount()){
            return num;
        }else{
            return "";
        }
    }

    /**
     * 处理numReduce请求
     */
    @RequestMapping("/numReduce")
    @ResponseBody
    public String numReduce(String num){
        //System.out.println(num);
        if(Integer.parseInt(num) <= 1){
            return num;
        }else{
            return "";
        }
    }
}

